package com.up4years.dataapi.service.system;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.up4years.dataapi.dao.base.PageDao;
import com.up4years.dataapi.dao.system.OrderMapper;
import com.up4years.dataapi.entity.base.Page;
import com.up4years.utils.DateUtil;

@Service
public class OrderService {
	Logger logger = Logger.getLogger(OrderService.class);
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private PageDao pageDao;
	
	public List<Map<String,Object>> list(Map<String,Object> parameters){
		return this.orderMapper.list(parameters);
	} 
	public Page<Map<String,Object>> pageList(Page<Map<String,Object>> page,Object parameters){
		String statementName = "com.up4years.dataapi.dao.system.OrderMapper.list";
		page= this.pageDao.pageQuery(page, statementName, parameters);
		List<Map<String,Object>> list = page.getResult();
		for(int i=0;i<list.size();i++){
			Map<String,Object> map = list.get(i);
			try {
				map.put("deadline", DateUtil.getDiff(new Date(),(Date)map.get("deadline")));
			} catch (ParseException e) {
				logger.error("deadline转换日期错误", e);
			}
		}
		return page;
 	}
	/**
	 * 接单
	 * @param parameters
	 * @return
	 */
	public boolean accept(Map<String, Object> parameters) {
		String order_no =(String) parameters.get("order_no");
		if(order_no.startsWith("SP")){
			return this.orderMapper.acceptShopOrder(parameters)==1;
		}else if(order_no.startsWith("EP")){
			return this.orderMapper.acceptExpressOrder(parameters)==1;
		}
		return false;
		
	}
	/**
	 * 查询
	 * @param order_no
	 * @return
	 */
	public Map<String, Object> getByOrderNo(String order_no) {
		if(order_no.startsWith("SP")){
			Map<String, Object>  order =this.orderMapper.getShopOrderByNo(order_no);
			if(order!=null&&null!=order.get("id")){
				List<Map<String,Object>> details = this.orderMapper.getShopOrderDetail((String)order.get("id"));
				order.put("details", details);
			}
			return order;
		}else if(order_no.startsWith("EP")){
			return this.orderMapper.getExpressOrderByNo(order_no);
		}
		return null;
 	}
	/**
	 * 取货
	 * @param order_no
	 * @return
	 */
	public boolean take(String order_no) {
		Map<String,Object> paramters =new HashMap<String,Object>();
		paramters.put("pick_time", new Date());
		paramters.put("order_no", order_no);
		if(order_no.startsWith("SP")){
			return this.orderMapper.takeShopOrder(paramters)==1;
		}else if(order_no.startsWith("EP")){
			return this.orderMapper.takeExpressOrder(paramters)==1;
		}
		return false;
	}
	public boolean arrive(String order_no,String accept_user,String finish_code) {
		Map<String,Object> paramters =new HashMap<String,Object>();
		paramters.put("completion_time", new Date());
		paramters.put("accept_user", accept_user);
		paramters.put("arrived_time", new Date());
		paramters.put("order_no", order_no);
		paramters.put("finish_code", finish_code);
		if(order_no.startsWith("SP")){
			return this.orderMapper.arriveShopOrder(paramters)==1;
		}else if(order_no.startsWith("EP")){
			return this.orderMapper.arriveExpressOrder(paramters)==1;
		}
		return false;
	}
	
}
